package com.ghn.cocknovel.ui.activity.map

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.basemodel.base.BaseFragment
import com.ghn.cocknovel.BR
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.FragmentDebusBinding
import com.ghn.cocknovel.ext.MappageLoadBase
import com.ghn.cocknovel.ui.activity.map.vm.MapViewModel
import com.kt.NetworkModel.bean.BQueryDriverTransitOrderListBean

class DebusFragment (val siteLatitude:Double,val siteLongitude:Double,val mTaskid:String,val type:Int): BaseFragment<FragmentDebusBinding, MapViewModel>() {
    val mWorkAdapter by lazy { MapPassengerAdapter() }
    override fun initVariableId(): Int {
        return  BR.mode
    }

    override fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): Int {
        return R.layout.fragment_debus
    }

    override fun initParam() {

    }

    override fun initViewObservable() {
        viewModel?.queryTransitOrder()
        viewModel?.queryPassengerBySite?.observe(this) {
            viewModel?.squeryDriverTransitOrderList(it?.siteId.toString(), 2, mTaskid)
        }
        viewModel?.queryDriverTransitOrderList?.observe(this) {
            binding?.mRefresh?.finishRefresh()
            MappageLoadBase(it, mWorkAdapter)
        }
        mWorkAdapter.bindToRecyclerView(binding?.debusRecyclerview)
        binding?.mRefresh?.setOnRefreshListener {
            viewModel?.queryTransitOrder()
        }
    }
    inner class MapPassengerAdapter : BaseQuickAdapter<BQueryDriverTransitOrderListBean, BaseViewHolder>,
        BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
        constructor() : super(R.layout.item_home, null) {
            onItemChildClickListener = this
            onItemClickListener = this
        }

        @SuppressLint("ResourceType")
        override fun convert(helper: BaseViewHolder?, item: BQueryDriverTransitOrderListBean?) {
            helper?.setText(R.id.item_home_author, item?.mobileLastFour)
            helper?.setText(R.id.item_home_author, "尾号"+item?.mobileLastFour)
            helper?.setText(R.id.item_home_originAddress,item?.originAddress)
            helper?.setText(R.id.item_home_destinationAddress,item?.destinationAddress)
            helper?.setText(R.id.item_home_PassengerNumber,"成人"+item?.adultNum+"人,"+"儿童"+item?.childrenNum+"人")
            when (item?.orderStatus) {
                1 -> {
                    helper?.setText(R.id.item_home_status, "待支付")
                }

                2 -> {
                    helper?.setText(R.id.item_home_status, "待呼叫")
                }

                3 -> {
                    helper?.setText(R.id.item_home_status, "呼叫中")
                }

                4 -> {
                    helper?.setText(R.id.item_home_status, "司机已接单")
                }

                5 -> {
                    helper?.setText(R.id.item_home_status, "司机到达")
                }

                6 -> {
                    helper?.setText(R.id.item_home_status, "已上车")
                }

                7 -> {
                    helper?.setText(R.id.item_home_status, "行程结束")
                }

                8 -> {
                    helper?.setText(R.id.item_home_status, "已取消")
                }

                9 -> {
                    helper?.setText(R.id.item_home_status, "已退款")
                }
            }
            if (type==1){
                helper?.setText(R.id.item_home_scar,"确认下车")
                helper?.setOnClickListener(R.id.item_home_scar,object :View.OnClickListener{
                    override fun onClick(p0: View?) {
                        if (helper.getView<TextView>(R.id.item_home_scar)?.text?.equals("确认下车")!!){
                            viewModel?.queryPassengerBySite?.observe(this@DebusFragment) {
                                viewModel?.arriveTransit(item?.orderId!!, it?.siteId!!, siteLatitude.toString(), siteLongitude.toString())
                                viewModel!!.arriveTransit.observe(this@DebusFragment) { it1 ->
                                    if (it1?.equals("确认成功")!!) {
                                        showMsg(it1)
                                        viewModel?.queryPassengerBySite?.observe(this@DebusFragment) {
                                            viewModel?.arriveTransit(item?.orderId!!, it?.siteId!!, siteLatitude.toString(), siteLongitude.toString())
                                        }
                                    }
                                }
                            }
                        }
                    }

                })

                }



//            helper?.setOnClickListener(R.id.item_home_cancel,object :View.OnClickListener{
//                override fun onClick(p0: View?) {
//                    viewModel?.queryPassengerBySite?.observe(this@DebusFragment) {
//                        viewModel?.cancelTimeoutOrder(item?.orderId!!)
//                        viewModel!!.cancelTimeoutOrder.observe(this@DebusFragment) { it1 ->
//                            if (it1?.equals("订单取消成功")!!) {
//                                showMsg(it1)
//                                viewModel?.queryPassengerBySite?.observe(this@DebusFragment) {
//                                    viewModel?.squeryDriverTransitOrderList(it?.siteId.toString(), 1, mTaskid)
//                                }
//                            }
//                        }
//                    }
//
//                }
//
//            })
        }

        override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        }

        override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        }
    }
}